package com.jellyleo.activiti.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jellyleo.activiti.entity.dto.ProcessDefinitionDTO;
import com.jellyleo.activiti.service.ProcessDefinitionService;
import com.woniu.config.RuoYiConfig;
import com.woniu.pojo.PageParam;
import com.woniu.until.ResponseData;
import com.woniu.until.file.FileUploadUtils;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService {

    @Autowired
    private RepositoryService repositoryService;

    public ResponseData<?> getProcessDefinitions(PageParam<ProcessDefinitionDTO> pageParam) {
        ProcessDefinitionDTO param = pageParam.getParam();
        List<ProcessDefinitionDTO> processDefinitions = new ArrayList<>();
        List<ProcessDefinition> definitions = repositoryService.createProcessDefinitionQuery().list();
        for (ProcessDefinition definition : definitions) {
            ProcessDefinitionDTO dto = new ProcessDefinitionDTO();
            dto.setId(definition.getId());
            dto.setName(definition.getName());
            dto.setKey(definition.getKey());
            dto.setVersion(definition.getVersion());
            dto.setDeploymentId(definition.getDeploymentId());
            dto.setResourceName(definition.getResourceName());
            // 根据需要设置suspendState
//             dto.setSuspendState(yourLogicHere);
            processDefinitions.add(dto);
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        PageInfo<ProcessDefinitionDTO> pageInfo = new PageInfo<>(processDefinitions);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public void getProcessDefineXML(HttpServletResponse response, String deploymentId, String resourceName) throws IOException {
        InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resourceName);
        int count = inputStream.available();
        byte[] bytes = new byte[count];
        response.setContentType("text/xml");
        OutputStream outputStream = response.getOutputStream();
        while (inputStream.read(bytes) != -1) {
            outputStream.write(bytes);
        }
        inputStream.close();
    }

    @Override
    public void addDeploymentByString(String stringBPMN) {
        repositoryService.createDeployment()
                .addString("CreateWithBPMNJS.bpmn", stringBPMN)
                .deploy();
    }

    @Override
    public String upload(MultipartFile multipartFile) throws IOException {
        return FileUploadUtils.upload(RuoYiConfig.getUploadPath() + "/processDefinition", multipartFile);
    }

    @Override
    public int deleteProcessDefinitionById(String id) {
        repositoryService.deleteDeployment(id, false);
        return 1;
    }
}